import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Query, Request } from '@nestjs/common';
import type { RequestWithUser } from 'src/type/common';
import { CreateUserDto } from './dto/create-user.dto';
import { FindAllUserDto } from './dto/find-all-user.dto';
import { UpdatePasswordDto } from './dto/update-password.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserService } from './user.service';

@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get('me')
  async findUserByToken(@Request() req: RequestWithUser) {
    return await this.userService.findOneByRoleAndMenu(req.user.userId);
  }

  @Get('page')
  async findAll(@Query() query: FindAllUserDto) {
    return this.userService.findAll(query);
  }

  @Put(':id')
  update(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto) {
    this.userService.update(id, updateUserDto);
    return '修改成功';
  }

  @Get(':id/form')
  findForm(@Param('id') id: number) {
    return this.userService.findForm(id);
  }

  @Delete(':id')
  async remove(@Param('id') id: number) {
    await this.userService.remove(id);
    return '删除成功';
  }

  @Patch(':id/password')
  async updatePassword(@Param('id') id: number, @Query() updatePasswordDto: UpdatePasswordDto) {
    await this.userService.updatePassword(id, updatePasswordDto.password);
    return '修改密码成功';
  }
}
